package com.flycard.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.flycard.order.Order;
import com.flycard.order.feign.ProductFeignClient;
import com.flycard.order.service.OrderService;
import com.flycard.product.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;


    @Autowired
    LoadBalancerClient loadBalancerClient;


    @Autowired
    ProductFeignClient productFeignClient;
    @SentinelResource(value = "createOrder",blockHandler = "createOrderFallBack")
    @Override
    public Order createOrder(long userId, long productId) {
//        Product product = getProductFromRemoteWithLoadBalancerAnnotation(productId);

        //使用feign
        Product product = productFeignClient.getProductById(productId);
        Order order=new Order();
        order.setId(1L);

        order.setUserId(userId);
        order.setNickName("张三");
        order.setAddress("上海");
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setProductList(Arrays.asList( product));
        return order;
    }


//兜底回调
    public Order createOrderFallBack(long userId, long productId, BlockException e){
        Order order=new Order();
        order.setId(0L);

        order.setUserId(userId);
        order.setNickName("XXXXXXX");
        order.setAddress("异常信息");
        order.setTotalAmount(new BigDecimal(0));
        order.setProductList(null);
        return order;
    }

    private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId){

        String url = "http://service-product/product/" + productId;

        log.info("远程请求路径:{" + url + "}");
        return restTemplate.getForObject(url, Product.class);
    }


    private Product getProductFromRemote(Long productId){
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");

        ServiceInstance instance = instances.get(0);

        String url = "http://"  + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求路径:{" + url + "}");
        return restTemplate.getForObject(url, Product.class);
    }


    private Product getProductFromRemoteWithLoadBalancer(Long productId){
       loadBalancerClient.choose("service-product");
       ServiceInstance instance = loadBalancerClient.choose("service-product");

        String url = "http://"  + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求路径:{" + url + "}");
        return restTemplate.getForObject(url, Product.class);
    }
}
